package fabuzhe_diyuezhe_model;

import com.activemq.Listener;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息的订阅者一
 * Created by sahngjun on 2017/4/7.
 */
public class JMSConsumer_Listener {

    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认的用户名
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默认的密码
    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认的连接地址

    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory;//连接工厂
        Connection connection = null;//连接
        Session session; //回话，接受或者是发送消息的线程
        Destination destination;//消息的目的地
        MessageConsumer messageConsumer;//消息的消费者

        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer_Listener.USERNAME, JMSConsumer_Listener.PASSWORD, JMSConsumer_Listener.BROKEURL);

        try {
            connection = connectionFactory.createConnection();  //通过工厂创建连接
            connection.start();         //启动连接
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//获得session
//            destination = session.createQueue("FirstQueue");//创建连接的消息队列
            destination = session.createTopic("firstTopic1");
            messageConsumer = session.createConsumer(destination);//创建消息消费者
            messageConsumer.setMessageListener(new Listener());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
